window.onload = function (){
    findByItem(1)

}


function changeFace(){
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL($("fileTxt").files[0]);
    reader.onload = function (ev) {
        $("faceImg").src = ev.target.result;
    }
}


function showQueryStu(){
    $("queryStuInfo").style.visibility="visible"
}



function findByItem(pageNO){
    axios.get("/student/findByItem",{
        params: {
            pageNO,
            name:$("stuName").value,
            className:$("stuClass").value,
            roomAddress:$("stuRoom").value
        }
    }).then(resp => {
        var info = resp.data;

        if (info === "noland"){
            location.href="/login.html?errorInfo=noland";
        }

        str="";
        info.list.forEach(n =>{
            str+=`<tr><td class="alt">${n.name}</td>
                      <td class="alt">${n.gender}</td>
                      <td class="alt">${n.classBean.className}</td>
                      <td class="alt">${n.roomBean.address}</td>
                      <td class="alt">${n.phone}</td>
                    <td class="alt"><input class="button" type="button" value="学生退房" onclick="delStu(${n.id})">
                    <input class="button" type="button" value="学生换房" onclick="changeRoom1(${n.id})">
                    <input class="button" type="button" value="详细信息" onclick="findById(${n.id})"></td></tr>`;
        })
        $("data").innerHTML=str;

        var cutStr="";
        for (var i=1;i<=info.totalPage;i++){
            cutStr+=`<a href="javascript:findByItem(${i})">${i}</a>`;
        }
        $("cutDiv").innerHTML=cutStr;
    })
}


function findById(id){
    $("showStu").style.visibility="visible";
    axios.get("/student/findById",{
        params:{
            id
        }
    }).then(resp => {
        var info = resp.data;

        $("showStuName").innerHTML=info.name;
        $("showStuGender").innerHTML=info.gender;
        $("showStuClass").innerHTML=info.classBean.className;
        $("showStuRoom").innerHTML=info.roomBean.address;
        $("showStuDate").innerHTML=info.date;
        $("showStuPhone").innerHTML=info.phone;
        $("showStuFace").src="/face/"+info.imgPath;
    })
}

function changeRoom1(id){
    $("changeStuRoom").style.visibility="visible";
    axios.get("/student/findById",{
        params:{
            id
        }
    }).then(resp =>{
        var info = resp.data;
        $("showStuIdC").value=info.id;
        $("showStuNameC").innerHTML=info.name;
        $("showStuGenderC").innerHTML=info.gender;
        $("showStuClassC").innerHTML=info.classBean.className;
        $("showStuRoomC1").innerHTML=info.roomBean.address;
        $("showStuFaceC").src="/face/"+info.imgPath;

        axios.get("/room/findByGender",{
            params:{
                gender:info.gender
            }
        }).then(resp =>{
            var info = resp.data;
            var str="";
            info.forEach(n =>{
                str+=`<option value="${n.id}">${n.address}</option>`;
            })
            $("showStuRoomC2").innerHTML=str;
        })
    })
}

function changeRoom2(){
    axios.get("/student/changeRoom",{
        params:{
            studentId:$("showStuIdC").value,
            roomId:$("showStuRoomC2").value
        }
    }).then(resp => {
        var info = resp.data;
        if (info == "ok"){

            backToStuMain();
            findByItem(1)
        }
    })
}


function backToStuMain(){
    $("addStuInfo").style.visibility="hidden";
    $("showStu").style.visibility="hidden";
    $("changeStuRoom").style.visibility="hidden";
    $("queryStuInfo").style.visibility="hidden";
}


function showAddStu(){
    $("addStuInfo").style.visibility="visible";
    $("faceImg").src="../../../../../face/0.jpg";
}

function showStuInfoByGender(value){
    axios.get("/room/findByGender",{
        params:{
            gender: value
        }
    }).then(resp => {
        var info = resp.data;
        var str="";
        info.forEach(n=>{
            str+=`<option value="${n.id}">${n.address}</option>`;
        })
        $("addStuRoom").innerHTML=str;

        axios.get("/class/findAll").then(resp => {
            var info = resp.data;
            var str="";
            info.forEach(n =>{
                str+=`<option value="${n.id}">${n.className}</option>`;
            })
            $("addStuClass").innerHTML=str;
        })
    })
}


function getSex(radioName){
    var obj=document.getElementsByName(radioName);
    for (var i=0;i<obj.length;i++){
        if (obj[i].checked){
            return obj[i].value;
        }
    }
    return "undefined";
}



async function addNewStu(){

    var formObj=new FormData();
    formObj.append("uploadImg",$("fileTxt").files[0]);

    let config ={
        headers:{'Content-type':'multipart/from-data'}
    }

    var uploadFileName;

    //向服务器上传文件，得到文件名
    await axios.post("/upload",formObj,config).then(resp => {
        uploadFileName = resp.data;
    })


    axios.get("/student/add",{
        params:{
            name:$("addStuName").value,
            gender:getSex("sex"),
            imgPath:uploadFileName,
            classId:$("addStuClass").value,
            roomId:$("addStuRoom").value,
            phone:$("addStuPhone").value
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            backToStuMain()
        }
    })
}


function delStu(id) {
    axios.get("/student/del",{
        params: {
            id
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            alert("删除成功");
            findByItem(1);
        }
    })
}

function findStu(){
    findByItem(1)
    backToStuMain()
}